<template>
  <div class="unit-list">
    <a-form layout="inline" class="default-search-form gb-flex-form" :form="form" @submit="handleSearch">
      <a-row :gutter="8">
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" label="单位编码">
            <a-input v-decorator="['unitCode']" placeholder="单位编码"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" label="单位名称">
            <a-input v-decorator="['unitName']" placeholder="单位名称"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item class="gb-search-button">
            <a-button class="item" v-if="funcs['UnitSearch']" type="primary" html-type="submit">
              {{ $t(funcs['UnitSearch']) }}
            </a-button>
            <a-button type="primary" class="item" @click="handleReset">
              {{ $t('reset') }}
            </a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <div class="table-multiple-operations">
      <UnitForm v-if="funcs['UnitCreate']" class="item" :text="$t(funcs['UnitCreate'])" :callback="handleSearch" url="/backend/unit/create" />
    </div>

    <a-table :columns="columns"
      :rowKey="record => record.id"
      :dataSource="data"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      size="small"
      :scroll="{ x: 1000 }"
    >
      <div slot="long_column_ellipsis" slot-scope="text">
        <LongTableColumn width="200">{{ text }}</LongTableColumn>
      </div>

      <div class="table-single-operations" slot="action" slot-scope="text, record">
        <Action :buttonInfo="buttonInfo" @actionWidth="handleActionWidth" :funcs="funcs">
          <template  v-if="funcs['UnitUpdate']" slot="UnitUpdate">
            <UnitForm v-if="funcs['UnitUpdate']" class="item" :text="$t(funcs['UnitUpdate'])" displayType="LINK" :row="record" :callback="handleSearch"  url="/backend/unit/update" />
          </template>
          <template  v-if="funcs['UnitDelete']" slot="UnitDelete">
            <Delete v-if="funcs['UnitDelete']" class="item" :text="$t(funcs['UnitDelete'])" displayType="LINK" :rows="[record]" :callback="handleSearch" url="/backend/unit/delete" />
          </template>
          <template  v-if="funcs['UnitEnable']" slot="UnitEnable">
            <UnitEnable v-if="funcs['UnitEnable']"  class="item" :text="$t(funcs['UnitEnable'])" displayType="LINK" :row="record" :callback="handleSearch" />
          </template>
          <template  v-if="funcs['UnitDisable']" slot="UnitDisable">
            <UnitDisable v-if="funcs['UnitDisable']"  class="item" :text="$t(funcs['UnitDisable'])" displayType="LINK" :row="record" :callback="handleSearch" />
          </template>
          <template v-if="funcs['UnitTest']" slot="UnitTest">
            <UnitTestList v-if="funcs['UnitTest']" class="item" :text="$t(funcs['UnitTest'])" displayType="LINK" :row="record" :callback="handleSearch" />
          </template>
        </Action>
      </div>
    </a-table>
  </div>
</template>
<script>
import axios from 'axios'
import common from '@/global/common.js'

export default {
  data() {
    return {
      form: this.$form.createForm(this, {name: this._uid}),
      data: [],
      loading: true,
      selectedRowKeys: [],
      selectedRows: [],
      pagination: {sortField: 'unit_name', sortOrder: 'ascend', ...common.getPagination()},
      actionWidth: 0,
    }
  },
  computed: {
    funcs() {
      return common.getVisibleFunc('UnitIndexList')
    },
    columns() {
      const columns = [
        { title: "单位编码", dataIndex: 'unitCode', sorter: true, key: 'unit_code', width: 100, },
        { title: "单位名称", dataIndex: 'unitName',  defaultSortOrder:'ascend', key: 'unit_name',width: 155,},
        { title: "服务器地址", dataIndex: 'serverIp', key: 'server_ip', width: 200,},
        { title: "服务器用户名", dataIndex: 'serverUserName', key: 'server_user_name', width: 100,},
        { title: "分析路径", dataIndex: 'analysisPath', key: 'analysis_path', width: 240,},
        { title: "并发提交数", dataIndex: 'concurrentLimit', key: 'concurrent_limit', width: 100,},
        { title: "状态", dataIndex: 'statusDesc', key: 'unit_status', width: 100,},
        { title: this.$t("create_date"), dataIndex: 'createTime', sorter: true, key: 'create_time',width: 155,},
        { title: this.$t("create_by"), dataIndex: 'createByDesc', key: 'create_by',width: 100,},
        { title: this.$t("update_date"), dataIndex: 'updateTime', sorter: true, key: 'update_time',width: 155,},
        { title: this.$t("update_by"), dataIndex: 'updateByDesc', key: 'update_by',width: 100,},
        { title: this.$t("action"), key: 'action', scopedSlots: { customRender: 'action' }, width: 165, fixed: 'right', }
      ]
      return columns
    },
    buttonInfo() {
      return {
        'UnitUpdate': 40,
        'UnitDelete': 40,
        'UnitEnable': 40,
        'UnitDisable': 40,
        'UnitTest': 40,
      }
    },
  },
  mounted() {
    this.fetch();
  },
  methods: {
    handleSearch (e) {
      if(e){
        this.pagination.current = 1;
        e.preventDefault();
      }
      this.fetch()
    },
    handleReset () {
      this.form.resetFields();
      this.handleSearch()
    },
    handleActionWidth (data) {
      this.actionWidth = data
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination.pageSize = pagination.pageSize
      this.pagination.current = pagination.current
      this.pagination.sortField = ''
      this.pagination.sortOrder = ''
      if (sorter.column) {
        this.pagination.sortField = sorter.columnKey
        this.pagination.sortOrder = sorter.order
      }
      this.fetch({
        ...filters,
      });
    },
    fetch (param = {}) {
      this.form.validateFields((err, formValue) => {
        if (err) {
          return;
        }
        this.loading = true
        axios({
          url: '/backend/unit/find',
          method: 'get',
          params: {
            pageSize: this.pagination.pageSize,
            pageNum: this.pagination.current,
            sortField: this.pagination.sortField,
            sortOrder: this.pagination.sortOrder,
            ...param,
            ...formValue
          },
        }).then((res) => {
          this.pagination.total = res.data.total;
          this.data = res.data.rows;
        }).catch(() => {
          
        }).finally(() => {
          this.loading = false;
        })
      });
    }
  },
}
</script>
<style>
.unit-list .ant-form-item-label {
  width: 120px;
}
</style>